package hl.demosample.app.ext

import android.text.TextUtils
import androidx.navigation.NavController
import hl.demosample.R
import hl.demosample.app.util.CacheUtil
import me.hgj.jetpackmvvm.ext.navigateAction
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException
import java.util.regex.Pattern

/*
* path: DemoSample 
* Author: 86133
* Create: 2021-06-01 14:44
* Changes (from 2021-06-01)
* Desc:
*/
/**
 * 获取进程号对应的进程名
 * @param pid 进程号
 * @return 进程名
 */
fun getProcessName(pid: Int): String? {
    var reader: BufferedReader? = null
    try {
        reader = BufferedReader(FileReader("/proc/$pid/cmdline"))
        var processName = reader.readLine()
        if (!TextUtils.isEmpty(processName)) {
            processName = processName.trim { it <= ' ' }
        }
        return processName
    } catch (throwable: Throwable) {
        throwable.printStackTrace()
    } finally {
        try {
            reader?.close()
        } catch (exception: IOException) {
            exception.printStackTrace()
        }

    }
    return null
}

/*** 拦截登录操作，如果没有登录跳转登录，登录过了贼执行你的方法 **/
fun NavController.jumpByLogin(action: (NavController) -> Unit) {
    if (CacheUtil.isLogin()) {
        action(this)
    } else {
        this.navigateAction(R.id.action_to_loginFragment)
    }
}

/*** 拦截登录操作，如果没有登录执行方法 actionLogin 登录过了执行 action ****/
fun NavController.jumpByLogin(
    actionLogin: (NavController) -> Unit,
    action: (NavController) -> Unit
) {
    if (CacheUtil.isLogin()) {
        action(this)
    } else {
        actionLogin(this)
    }
}

/*** 验证手机号码 ***/
fun checkMobileAll(mobile: String): Boolean {
    val p = Pattern.compile("^[1][3-9]\\d{9}$|^([6|9|5])\\d{7}$|^[6]([8|6])\\d{5}$")
    val m = p.matcher(mobile)
    return m.matches()
}




